/*
 * This file is part of the Android Dimensions Editor Project: http://code.google.com/p/android-dimension-editor/
 *  
 * Copyright (c) 2013 Rick Falck
 *  
 * This code is licensed under the terms of the Apache License Version 2.0.
 * You may use this code according to the license.
 *
 * The terms of the license can be found in the root directory of this project's repository as well as at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under these Licenses is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See each License for the specific language governing permissions and
 * limitations under that License.
 * 
 * The icons used by the MessageBox are from ColoCons: www.MouseRunner.com
 * They are licensed under the Creative Commons Attribution-ShareAlike 2.5 License.
 *
 * Visit the address below to learn more about the Creative Commons license.
 * http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode
 *
 */
package MessageBox;

import java.util.ArrayList;

import model.ReturnInfo;
import MessageBox.MessageBoxController.MBOX_TYPE;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * <b><i>public class MessageBox</i></b>
 * <p>
 * This class provides a static method to display a pop-up message, based on
 * information passed to the showPopUp() function.
 *
 * @author Rick Falck
 */
public class MessageBox {

    /**
     * <b><i>public static ReturnInfo showPopUp(Stage primaryStage, MBOX_TYPE
     * mType, String messageText, ArrayList<String> folderNames)</i></b>
     * <p>
     * This method sets up the messagebox to display, displays it, and returns
     * the results to the caller in ReturnInfo.<p>
     *
     * <b>ReturnInfo.isSuccess()</b> returns true if the user clicked the OK
     * button, or false if they clicked cancel or an error occurred.<p>
     * <b>ReturnInfo.getOtherInfo()</b> - returns the string entered by the user
     * or the selection from the combobox.<p>
     * <b>ReturnInfo.getErrorMessage()</b> - Has error text if an error
     * occurred.<br>
     *
     * @param primaryStage - The Stage that is to be the parent of this
     * messagebox.
     * @param mType - The type of messagebox to display. This determines the css
     * style, title text, and icon used. If GET_USER_SELECTION, the folderNames
     * must be set.
     * @param messageText - The text to display.
     * @param folderNames - For GET_USER_SELECTION messages, contains the
     * strings to display in the combobox.
     * @return ReturnInfo
     */
    public static ReturnInfo showPopUp(Stage primaryStage, MBOX_TYPE mType, String messageText, ArrayList<String> folderNames) {
        ReturnInfo retInfo = new ReturnInfo();
        MessageBox mMessageBox = new MessageBox();
        try {
            switch (mType) {
                case CONFIRMATION:
                    mMessageBox.setTitleBarText("Confirmation");
                    break;
                case GET_USER_TEXT:
                    mMessageBox.setTitleBarText("Get Folder Name");
                    break;
                case GET_USER_SELECTION:
                    mMessageBox.setTitleBarText("Get Folder Name");
                    mMessageBox.setComboBoxStrings(folderNames);
                    break;
                case ERROR:
                    mMessageBox.setTitleBarText("Error");
                    break;
                default:
                case INFORMATION:
                    mMessageBox.setTitleBarText("Info");
                    break;
            }
            mMessageBox.setMessage(messageText);
            mMessageBox.setMsgType(mType);
            mMessageBox.setModality(Modality.WINDOW_MODAL);

            boolean response;
            if (mType == MBOX_TYPE.HELP) {
                response = mMessageBox.showWebView(primaryStage);
            } else {
                response = mMessageBox.showMessageBox(primaryStage);
            }
            retInfo.setSuccess(response);

            switch (mType) {
                case CONFIRMATION:
                case ERROR:
                case INFORMATION:
                case HELP:
                    return retInfo;
                case GET_USER_TEXT: // Create new Values folder
                    if (response) {
                        String newFolder = MessageBox.mUserText.trim();
                        newFolder = newFolder.toLowerCase();
                        retInfo.setOtherInfo(newFolder);
                    }
                    return retInfo;
                case GET_USER_SELECTION:
                    if (response) {
                        retInfo.setOtherInfo(mComboBoxSelectedText);
                    }
                    return retInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
            retInfo.setSuccess(false);
            retInfo.setErrorMessage(e.getMessage());
        }
        return retInfo;
    }

    protected Stage parentStage = null;
    protected Stage myStage;
    protected MBOX_TYPE mbType = null;
    protected String titleBarText = null;
    protected String message = null;
    protected Modality modality = Modality.WINDOW_MODAL;
    protected MessageBoxController mbController;
    protected WebViewController mWVController;
    protected String myFXMLFilePath = "MessageBoxFXML.fxml";
    protected String myWebViewFXMLFilePath = "WebViewFXML.fxml";
    protected String sMessageTextStyle;
    protected ArrayList<String> mComboBoxStrings;
    public static String mComboBoxSelectedText;
    public static String mUserText;

    private MessageBox() {
    }

    private MessageBox(MBOX_TYPE mType, String sTitle, String sMessage, Modality modality) {
        this.mbType = mType;
        this.titleBarText = sTitle;
        this.message = sMessage;
        this.modality = modality;
    }
    
    private boolean showWebView(Stage parentStage) {
        this.parentStage = parentStage;
        try {
            myStage = new Stage();
            FXMLLoader fLoader = new FXMLLoader(WebViewController.class.getResource(myWebViewFXMLFilePath));
            this.mWVController = new WebViewController(myStage);
            fLoader.setController(this.mWVController);
            AnchorPane myPage = (AnchorPane) fLoader.load();

            Scene myScene = new Scene(myPage);

            myStage.setScene(myScene);
            myStage.setTitle(this.titleBarText);
            myStage.initOwner(this.parentStage);
            myStage.initModality(this.modality);
            myStage.showAndWait();
        } catch (Exception ex) {
            System.out.println("Exception found in showMessageBox");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    private boolean showMessageBox(Stage parentStage) {
        this.parentStage = parentStage;

        try {
            myStage = new Stage();
            FXMLLoader fLoader = new FXMLLoader(MessageBoxController.class.getResource(myFXMLFilePath));
            this.mbController = new MessageBoxController(myStage);
            this.mbController.setMessage(message);
            this.mbController.setMbType(this.mbType);
            this.mbController.setComboBoxStrings(mComboBoxStrings);
            fLoader.setController(this.mbController);
            AnchorPane myPage = (AnchorPane) fLoader.load();

            Scene myScene = new Scene(myPage);

            switch (this.mbType) {
                case CONFIRMATION:
                    myScene.getStylesheets().add("MessageBox/MessageBoxConfirm.css");
                    break;
                case GET_USER_TEXT:
                case GET_USER_SELECTION:
                    myScene.getStylesheets().add("MessageBox/MessageBoxGetInfo.css");
                    break;
                case ERROR:
                    myScene.getStylesheets().add("MessageBox/MessageBoxError.css");
                    break;
                default:
                case INFORMATION:
                    myScene.getStylesheets().add("MessageBox/MessageBoxInfo.css");
                    break;
            }

            myStage.setScene(myScene);
            myStage.setTitle(this.titleBarText);
            myStage.initOwner(this.parentStage);
            myStage.initModality(this.modality);
            myStage.showAndWait();
            mComboBoxSelectedText = mbController.getComboBoxSelectedText();
            mUserText = mbController.getTextFieldText();
            return this.mbController.userClickedOK();
        } catch (Exception ex) {
            System.out.println("Exception found in showMessageBox");
            ex.printStackTrace();
            return false;
        }
    }

    public void setMsgType(MBOX_TYPE mBoxType) {
        this.mbType = mBoxType;
    }

    public void setTitleBarText(String titleBarText) {
        this.titleBarText = titleBarText;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setModality(Modality modality) {
        this.modality = modality;
    }

    public void setMyFXMLFilePath(String myFXMLFilePath) {
        this.myFXMLFilePath = myFXMLFilePath;
    }

    public void setsMessageTextStyle(String sMessageTextStyle) {
        this.sMessageTextStyle = sMessageTextStyle;
    }

    public void setComboBoxStrings(ArrayList<String> comboBoxStrings) {
        mComboBoxStrings = comboBoxStrings;
    }
}
